package com.sansecy.customview;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;

import com.sansecy.customview.tab.BlankFragment;
import com.sansecy.customview.tab.CustomTabLayout;

import java.util.ArrayList;
import java.util.Collections;

public class TabActivity extends AppCompatActivity {
    private String[] mTitles = new String[]{"简介", "评价", "相关", "相关", "相关", "相关", "相关"};
    private static final String TAG = "TabActivity-App";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        CustomTabLayout customTabLayout = findViewById(R.id.ucstomTabLayout);
        ArrayList<String> strings = new ArrayList<>();
        Collections.addAll(strings, mTitles);
        customTabLayout.setScrollMode(CustomTabLayout.MODE_SCROLLABLE);
        customTabLayout.setTitle(strings);
        customTabLayout.setIndicatorHeight(5);
        customTabLayout.setTextSize(27);
        customTabLayout.setTextPadding(100, 0, 100, 0);
        customTabLayout.setIndicatorWidth(50);
        customTabLayout.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return new BlankFragment();
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Log.d(TAG, "onPageSelected: " + position);
            }
        });
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
        }
        tabLayout.setupWithViewPager(viewPager);
        customTabLayout.setupWithViewPager(viewPager);
    }
}
